<template>
  <div class="goods">
    <!-- 添加按钮 -->
    <el-button @click="showAdd" type="success" plain class="addBtn">添加商品</el-button>
    <!-- 商品列表 -->
    <el-table :data="goodsList" style="width: 100%" class="goodTable">
      <el-table-column label="#" type="index" :index="indexMethod"></el-table-column>
      <el-table-column prop="goods_name" label="商品名称"></el-table-column>
      <el-table-column prop="goods_price" label="商品价格"></el-table-column>
      <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
      <el-table-column label="创建时间">
        <template v-slot="{row}">
          {{ row.add_time | time }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{row}">
          <el-button type="primary" plain size="small" icon="el-icon-edit"></el-button>
          <el-button type="danger" plain size="small" icon="el-icon-delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      goodsList: [],
      pnp: '',
      pagenum: 1,
      pagesize: 5,
      total: 0
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const { meta, data } = await this.$axios.get(`goods`, {
        params: {
          pnp: '',
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      })
      if (meta.status === 200) {
        this.total = data.total
        this.goodsList = data.goods
        console.log(this.goodsList)
      } else {
        this.$message.error(meta.msg)
      }
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.pagenum = 1
      this.getGoodsList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.pagenum = val
      this.getGoodsList()
    },
    // 每页索引连接排序
    indexMethod (index) {
      return index + 1 + (this.pagenum - 1) * this.pagesize
    },
    // 显示添加页面
    showAdd () {
      this.$router.push('/goods-add')
    }
  }
}
</script>

<style lang="scss" scoped>
.addBtn {
  margin-bottom: 10px;
}
.goodTable {
  margin-bottom: 10px;
}
</style>
